var CheXingCityTable;
/****列表信息*/
function showChexCityList() {
	showLoading("查询中...");
	$.ajax( {
		url : 'chexingCity/listPage.do',
		type : 'get',
		async:true,
		dataType : 'html',
		success : function(html) {
			$('#mainForm').html(html);
			queryChexCityDatas();
			hideLoading();
		}
	});
}

function queryChexCityDatas(){
		CheXingCityTable = $('#entTable').table( {
		width : '100%',
		form : {
			url : 'chexingCity/queryPage.do',
			id : '#entSearchForm',
			type : 'post',
			pageNumberText : '#pageNum'
		},
		pageable : true,
		selectable : true,
		colums : [ {
			columnName : '城市',
			name : 'city'
		//	handle : "toEdditCheXingcity"
		}, {
			columnName : '车型名称',
			name : 'name'
		}, {
			columnName : '省',
			name : 'province'
		}, {
			columnName : '更新时间',
			name : 'updateDate'
		} ,
		{columnName:'按城市编辑',name:'XXX',sortable:false, width:'100px',formatter:function(obj){
		  			return "<input type='button' style='cursor: pointer' value='编辑' onclick='toEditByCity(\""+obj.city+"\",\""+obj.province+"\")'>";}
		}]
	});
}
//查询城市车型分页信息
function queryChexCityPage() {
	showLoading("查询中...");
	queryChexCityDatas();
	hideLoading();
}

function toEditByCity(city, province) {
	$.ajax( {
		url : 'chexingCity/toEditByCity.do?t=' + (new Date()),
		type : 'post',
		data : {
			'province' : province,
			'city' : city
		},
		dataType : 'html',
		success : function(html) {
			$('#commonDialogDiv').html(html);
			$('#commonDialogDiv').dialog( {
				modal : true,
				title : '城市车型编辑',
				width : 852,
				height : 580,
				buttons : {
					'保存' : function() {
						saveAddCheXingCity();
					},
					'取消' : function() {
						$(this).dialog('close');
					}
				},close:function(){$(this).dialog('destroy');}
			});
			$( "#chexingTable tbody" ).sortable({ 
			 axis: 'y' ,
			 tolerance: 'pointer',
			 cursor: 'crosshair',
			 helper: fixHelper,
			 revert: true,
			 cursor: 'move', //拖动的时候鼠标样式 
			 containment: 'parent',
			 delay:100,
			 start:function(e, ui){ 
                    ui.helper.addClass("moveAble");
                    return ui; 
             }, 
             stop:function(e, ui){ 
             	var i=0;
             	$(".records tr").each(function(){
             		$(this).removeClass("r1").removeClass("r2");
             		if(i%2==0){
             			$(this).addClass("r1");
             		}else{
             			$(this).addClass("r2");
             		}
             		i++;
             	});
                 ui.item.removeClass("ui-state-highlight"); //释放鼠标时，要用ui.item才是释放的行  
                 ui.item.removeClass("moveAble");
                 return ui; 
             }  
		}).disableSelection();
			
			validator = $("#CheXingForm").validate({});
		}
	});
}

function toEdditCheXingcity(id) {
	var param = '?id='+id+'&t=' + ts();
	$.get('chexingCity/toUpdateCheXingCity.do' + param, function(html) {
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '城市车型查看',
			width : 852,
			height : 320,
			buttons : {
				'保存' : function() {
					saveUpdateCheXingCity();
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			},close:function(){$(this).dialog('destroy');}
		});
		
		validator = $("#CheXingForm").validate({});
	});
}

/**保存单条编辑信息***/
function saveUpdateCheXingCity() {
	if (validator.form()) {
		$('#CheXingForm').ajaxSubmit( {
			url : 'chexingCity/saveUpdateCheXingCity.do',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					showDialog('保存成功');
					$('#commonDialogDiv').dialog('close');
					queryChexCityPage();
				} else {
					showDialog('保存失败' + data.message);
				}
			}
		});
	}
}

//去增加
function toAddChexCity(id) {
	var param = '?t=' + ts();
	$.get('chexingCity/toAddCheXingCity.do' + param, function(html) {
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '城市车型添加',
			width : 852,
			height : 580,
			buttons : {
				'保存' : function() {
					saveAddCheXingCity();
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			},close:function(){$(this).dialog('destroy');}
		});
		$( "#chexingTable tbody" ).sortable({ 
			 axis: 'y' ,
			 tolerance: 'pointer',
			 cursor: 'crosshair',
			 helper: fixHelper,
			 revert: true,
			 cursor: 'move', //拖动的时候鼠标样式 
			 containment: 'parent',
			 delay:100,
			 start:function(e, ui){ 
                    ui.helper.addClass("moveAble");
                    return ui; 
             }, 
             stop:function(e, ui){ 
             	var i=0;
             	$(".records tr").each(function(){
             		$(this).removeClass("r1").removeClass("r2");
             		if(i%2==0){
             			$(this).addClass("r1");
             		}else{
             			$(this).addClass("r2");
             		}
             		i++;
             	});
                 ui.item.removeClass("ui-state-highlight"); //释放鼠标时，要用ui.item才是释放的行  
                 ui.item.removeClass("moveAble");
                 return ui; 
             }  
		}).disableSelection();
		validator = $("#CheXingForm").validate( {
			rules : {
				"province" : {
					required : true
				},
				"city" : {
					required : true
				}
			},
			messages : {
				"province" : {
					required : "必选"
				},
				"city" : {
					required : "必选"
				}
			}
		});
	});
}
//去保存
function saveAddCheXingCity() {
	if (validator.form()) {
		$('#CheXingForm').ajaxSubmit( {
			url : 'chexingCity/saveCheXingCity.do',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					showDialog('保存成功');
					$('#commonDialogDiv').dialog('close');
					queryChexCityPage();
				} else {
					showDialog('保存失败' + data.message);
				}
			}
		});
	}
}

//批量删除 
function toDeleteChexCity() {
	var ids = '';
	$('.ids').each(function() {
		if (this.checked == true) {
			ids += 'ids=' + this.value + '&';
		}
	});
	if (ids.length == 0) {
		showDialog('请至少选择一条记录！');
		return;
	}
	$('#commonDialogDiv').html('<p>确定要删除记录信息？</p>');
	$('#commonDialogDiv').dialog( {
		modal : true,
		width : 250,
		height : 160,
		buttons : {
			'确定' : function() {
				$(this).dialog('close');
				$.ajax( {
					url : 'chexingCity/delete.do?' + ids,
					type : 'get',
					dataType : 'json',
					success : function(data) {
						if (data.success) {
							queryChexCityPage();
						} else {
							showDialog('删除失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function() {
				$(this).dialog('close');
			}
		},close:function(){$(this).dialog('destroy');}
	});
}

function getCityChexing(obj,shiId){
				if($(obj).val()==''){
					$("#"+shiId).empty();
					$("#"+shiId).html("<option value=''>选择市</option>");
					return;
				}
				$.ajax({
					url:'district/queryCityByProvince.do?t='+(new Date()),
					type:'post',
					data:{pname:$(obj).val()},
					dataType:'json',
					success:function(json){
						var shis = json;
						var html = "<option value=''>选择市</option>";
						for(var i=0;i<shis.length;i++){
							html +="<option value='"+shis[i].name+"'>"+shis[i].name+"</option>";
						}
						$('#'+shiId).html(html).parent().css('display','');
						 $("input[name='ids']").each(function(){
							 $(this).attr("checked",false);
						 });
					}
				});
			}
//勾选已经选择的车型
function checkChexing(id) {
	if(id){
		$.ajax( {
		url : 'chexingCity/queryCityChexingIds.do?t=' + (new Date()),
		type : 'post',
		data : {
			'city' : $(id).val()
		},
		dataType : 'json',
		success : function(json) {
			if(json.success){
				$("input[name='ids']").each(function(){
					 $(this).attr("checked",false);
				    });
				if(json.result.length>0){
					for(var i=json.result.length-1;i>=0;i--){
						var table1 = $('#chexingTable'); 
					    var firstTr = table1.find('tbody>tr:first'); 
					    var newTr=$("#tr_"+json.result[i]);
					    var index=$("#tr_"+json.result[i]).index();
					    if(index>0){
					    	table1.find('tbody>tr:first');
							firstTr.before(newTr);
					    }
					    $("#CheckBox_"+json.result[i]).attr("checked",true);
				    }
				}
			}else{
				 $("input[name='ids']").each(function(){
					 $(this).attr("checked",false);
				 });
			}
		}
	});
	}
}